﻿/*
 * @author: wizardc
 */

using UnityEditor;
using UnityEngine;

namespace DouEditor.Extend
{
    [CustomEditor(typeof(MixedQuickViewer))]
    public class MixedQuickViewerEditor : Editor
    {
        private MixedQuickViewer _viewer;

        private SerializedProperty _animatorList;
        private SerializedProperty _particleList;

        private void Awake()
        {
            _viewer = (MixedQuickViewer) target;

            _animatorList = serializedObject.FindProperty("animatorList");
            _particleList = serializedObject.FindProperty("particleList");
        }

        public override void OnInspectorGUI()
        {
            if (_viewer.playing)
            {
                Repaint();
            }

            EditorGUI.BeginDisabledGroup(true);
            {
                EditorGUILayout.PropertyField(_animatorList, new GUIContent("被控制的动画组件"));
                EditorGUILayout.PropertyField(_particleList, new GUIContent("被控制的特效组件"));
            }
            EditorGUI.EndDisabledGroup();

            using (new GUILayout.VerticalScope())
            {
                GUILayout.Label("动画播放", EditorStyles.boldLabel);

                var color = GUI.color;

                using (new GUILayout.HorizontalScope())
                {
                    _viewer.isLoop = GUILayout.Toggle(_viewer.isLoop, "循环");
                    GUI.color = Color.green;
                    if (GUILayout.Button("播放"))
                    {
                        _viewer.Play();
                    }
                    if (_viewer.playing)
                    {
                        GUI.color = Color.cyan;
                        if (GUILayout.Button("暂停"))
                        {
                            _viewer.Pause();
                        }
                    } else
                    {
                        GUI.color = Color.yellow;
                        if (GUILayout.Button("恢复"))
                        {
                            _viewer.Resume();
                        }
                    }
                    GUI.color = Color.red;
                    if (GUILayout.Button("停止"))
                    {
                        _viewer.Stop();
                    }
                }

                GUI.color = color;

                _viewer.ratio = EditorGUILayout.Slider("播放速率", _viewer.ratio, 0.1f, 10f);

                GUILayout.Label("时间控制", EditorStyles.boldLabel);
                EditorGUILayout.HelpBox($"\n总时长: {_viewer.totalTime}s\n", MessageType.Info);
                var oldTime = Mathf.Clamp((float) _viewer.currentTime, 0, (float) _viewer.totalTime);
                var newTime = (float) EditorGUILayout.Slider(GUIContent.none, oldTime, 0, (float) _viewer.totalTime);
                if (Mathf.Abs(newTime - oldTime) > 0.01f)
                {
                    if (_viewer.playing)
                    {
                        _viewer.Pause();
                    }
                    _viewer.currentTime = newTime;
                }

                GUILayout.Label("帧控制", EditorStyles.boldLabel);
                _viewer.frameRate = EditorGUILayout.IntSlider("帧率", _viewer.frameRate, 1, 120);
                EditorGUILayout.HelpBox($"\n总帧数: {_viewer.totalFrame}\n", MessageType.Info);
                var oldFrame = Mathf.Clamp(_viewer.currentFrame, 0, _viewer.totalFrame - 1);
                var newFrame = EditorGUILayout.IntSlider(GUIContent.none, _viewer.currentFrame, 0, _viewer.totalFrame - 1);
                if (oldFrame != newFrame)
                {
                    if (_viewer.playing)
                    {
                        _viewer.Pause();
                    }
                    _viewer.currentFrame = newFrame;
                }
            }
        }
    }
}
